﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Web.SessionState;

namespace SES.CMS
{
    public partial class Tag : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Tag - VPP";
            loadNews();
        }
        private void loadNews()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                string key = Request.QueryString["ID"];
                DataTable dt = new cmsSanPhamBL().SelectbyTagItem(key);
                bindatalist(dt);
            }
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            //objPds.AllowPaging = true;
            //objPds.PageSize = 5;
            // objPds.CurrentPageIndex = CurrentPage;
            //cmdPrev.Visible = !objPds.IsFirstPage;
            //cmdNext.Visible = !objPds.IsLastPage;
            rpt.DataSource = objPds;
            rpt.DataBind();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNews();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNews();
        }

        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsGioHangDO obj = new cmsGioHangBL().SelecBySessionAndSPNonXL(session, id);
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                if (obj.GioHangID < 1)
                {
                    obj = init();
                    obj.SessionID = session;
                    obj.SanPhamID = id;
                    obj.ThanhTien = gia * obj.SoLuong;
                    new cmsGioHangBL().Insert(obj);

                    Ultility.Alert("Đặt mua thành công!", url);
                }

                else
                {
                    obj.SoLuong++;
                    obj.ThanhTien += gia;
                    new cmsGioHangBL().Update(obj);
                    Ultility.Alert("Đặt mua thành công!", url);
                }
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
        }

    }
}
